<?php
/**
 * Created by PhpStorm.
 * User: sqq
 * Date: 2018/07/11
 * Time: 上午11:35
 */
class AhpService extends Eloquent
{
    protected $table = "ahp_service";


    public function desc(){
        return $this->hasMany('AhpServiceDesc',"service_id");
    }

    public function ahp_users(){
        return $this->hasOne('AhpUsers',"id","updated_by");
    }

    /**
     * @Author:      sqq
     * @DateTime:    2018-07-28 11:38:16
     * @Description: 活动包计算价格
     */
    public static function ProductActivity($pids,$sc_user,$wmcf,$whp,$pa,$csi,$car_model_id,$city_id){
        $total=0;
        foreach($pids as $pi){
            $count_amount=0;
            $package=ProductPackage::find($pi['package_id']);
            if(is_null($package)){
                $response = Lang::get("scs_error.package_error_50001");
                return $response;break;
            }
            if($package->status!=1){
                $response = Lang::get("scs_error.package_status_error_50002");
                return $response;break;
            }
            if($package->place!=2){
                $response = Lang::get("scs_error.package_unable_error_50003");
                return $response;break;
            }
            $p_item=ProductPackage::getItemCS5($car_model_id,$package->id);
            $supply_arr=$p_item['supply'];
            $order_product_item=array();
            foreach ($supply_arr as $_sa) {

                $papi = ProductActivityPackageItem::where("subject",$_sa["subject"])->where("product_activity_package_id",$pi['id'])->first();
                if (!empty($papi)) {
                    $item_info = SupplyItem::getCacheItemInfo($papi->supply_item_id);
                    $_sa['number'] = $papi->number;
                    $_sa['name'] = $item_info->name;
                    $_sa['id'] = $papi->supply_item_id;
                    $_sa['subject'] = $item_info->subject;
                    $_sa['brand_id'] = $item_info->brand_id;
                    $_sa['amount'] = $item_info->amount;
                    $_sa['series_name'] = $item_info->series_name;
                    $_sa['model'] = $item_info->model;
                    $_sa['unit'] = $item_info->unit;
                    $_sa['is_adapter'] = 1;
                }

                //更换耗材的加价系数

                $wpc=WorkPartsCoefficient::where("car_model_name",$csi->model_level)->where("supply_brand_id",$_sa["brand_id"])->where("supply_subject_id",$_sa["subject"])->where("status",1)->first();
                $wmcf->supply_coefficient = null;
                if (!empty($wpc)) {
                    $wpc_city=WorkPartsCoefficientCity::where('city_id',$city_id)->where('coefficient_id',$wpc->id)->where('status',1)->first();
                    $wmcf->supply_coefficient=isset($wpc_city->coefficient) ? $wpc_city->coefficient : null; //如果有城市系数，优先使用。
                }
                if ($wmcf->supply_coefficient == null){ //如果是0，说明没有赋值
                    $wmcf->supply_coefficient=isset($wpc->coefficient) ? $wpc->coefficient : 1;
                }

                //更换多城市价格
                // $sip=SupplyItemPrice::where("item_id",$_sa["id"])->where("city_id",$sc_user->shop->city_id)->first();
                // if($sip){
                //     $_sa["amount"]=$sip->price;
                // }
                $sip=SupplyCityPrice::where("supply_id",$_sa["id"])->where("city_id",$city_id)->where("status",1)->first();
                if($sip){
                    $_sa["amount"]=$sip->price;
                }else{
                    $ss_temp=SupplySub::where("supply_id",$_sa["id"])->where("status",1)->first();
                    $_sa["amount"]=isset($ss_temp)?$ss_temp->usual_price:0;
                }

                $temp_amount=$_sa["amount"]*$wmcf->supply_coefficient;
                $temp_amount=Common::upFow($temp_amount);
                $order_product_item[]=array(
                    "item_id"=>$_sa["id"],
                    "item_name"=>$_sa["name"],
                    "amount"=>$temp_amount,
                    "num"=>$_sa["number"],
                    "type"=>1,
                    "unit"=>$_sa["unit"],
                    "is_adapter"=>$_sa["is_adapter"]
                );
                $count_amount+=$temp_amount*$_sa["number"];
                $total+=$temp_amount*$_sa["number"];
            }

            foreach ($p_item['server'] as $_se) {
                $res=PricingStrategy::getStringStrategy($csi->id,$_se['id'],$city_id);
                if($res['code']==0){
                    $temp_amount=$res['price'];
                }else{
                    //判断工时类型为工时类型还是固定价格
                    if($_se['type'] ==1 ){
                        $temp_amount=$whp->price*$wmcf->work_coefficient*$_se["work_hours"];
                    }elseif($_se['type']==2){
                        $temp_amount=$_se["price"];
                    }
                }
                $order_product_item[]=array(
                    "item_id"=>$_se["id"],
                    "item_name"=>$_se["name"],
                    "amount"=>$temp_amount,
                    "num"=>$_se["work_hours"],
                    "type"=>2,
                    "unit"=>"个",
                    "is_adapter"=>1
                );
                $count_amount+=$temp_amount;
                $total+=$temp_amount;
            }
        }

        $response =  array(
            'count_amount' => $total * $pa->discount,
            "price"=>$total,
            "discount_rate"=>$pa->discount,
        );
        return $response;
    }



    /**
     * @Author:      sqq
     * @DateTime:    2018-08-03 09:48:16
     * @Description: 虚拟单添加活动包
     */
    public static function addProductActivity($pids,$sc_user,$orders,$wmcf,$whp,$pa,$csi,$city_id){
        return DB::transaction(function () use ($pids,$sc_user,$orders,$wmcf,$whp,$pa,$csi,$city_id) {
            $order_product=new OrderProduct;
            $order_product->order_id=$orders->order_id;
            $order_product->product_type="activity";
            $order_product->product_id=$pa->id;
            $order_product->product_name=$pa->name;
            $order_product->is_edit=$pa->is_edit;
            $order_product->status=1;
//            if($pa->type==1){
//                $order_product->amount=$pa->price;
//            }else{
//                $order_product->amount=$pa->price;
//            }
            $order_product->amount=$pa->price;
            $order_product->counter_amount=$pa->price * $pa->discount;
            $order_product->save();
            $order=Orders::where("order_id",$orders->order_id)->lockForUpdate()->first();
            $total=0;
            foreach($pids as $pi){
                $count_amount=0;
                $package=ProductPackage::find($pi['package_id']);
                if(is_null($package)){
                    DB::rollback();
                    $response = Lang::get("scs_error.package_error_50001");
                    return $response;
                }
                if($package->status!=1){
                    DB::rollback();
                    $response = Lang::get("scs_error.package_status_error_50002");
                    return $response;
                }
                if($package->place!=2){
                    DB::rollback();
                    $response = Lang::get("scs_error.package_unable_error_50003");
                    return $response;
                }
                $p_item=ProductPackage::getItemCS5($orders->car_model_id,$package->id);
                Log::info("info", array("system" => "api3.0" ,"event" => "测试活动包", 'input' =>Input::all() ,"output" => $p_item));
                $supply_arr=$p_item['supply'];
                $order_product_item=array();
                foreach ($supply_arr as $_sa) {

                    $papi = ProductActivityPackageItem::where("subject",$_sa["subject"])->where("product_activity_package_id",$pi['id'])->first();
                    if (!empty($papi)) {
                        $item_info = SupplyItem::getCacheItemInfo($papi->supply_item_id);
                        $_sa['number'] = $papi->number;
                        $_sa['name'] = $item_info->name;
                        $_sa['id'] = $papi->supply_item_id;
                        $_sa['subject'] = $item_info->subject;
                        $_sa['brand_id'] = $item_info->brand_id;
                        $_sa['amount'] = $item_info->amount;
                        $_sa['series_name'] = $item_info->series_name;
                        $_sa['model'] = $item_info->model;
                        $_sa['unit'] = $item_info->unit;
                        $_sa['is_adapter'] = 1;
                        $_sa['discount']=$pa->discount;
                        $_sa['counter_amount']=$pa->price * $item_info->amount;
                    }

                    //更换耗材的加价系数

                    $wpc=WorkPartsCoefficient::where("car_model_name",$csi->model_level)->where("supply_brand_id",$_sa["brand_id"])->where("supply_subject_id",$_sa["subject"])->where("status",1)->first();
                    $wmcf->supply_coefficient = null;
                    if (!empty($wpc)) {
                        $wpc_city=WorkPartsCoefficientCity::where('city_id',$city_id)->where('coefficient_id',$wpc->id)->where('status',1)->first();
                        $wmcf->supply_coefficient=isset($wpc_city->coefficient) ? $wpc_city->coefficient : null; //如果有城市系数，优先使用。
                    }
                    if ($wmcf->supply_coefficient == null){ //如果是0，说明没有赋值
                        $wmcf->supply_coefficient=isset($wpc->coefficient) ? $wpc->coefficient : 1;
                    }

                    //更换多城市价格
                    // $sip=SupplyItemPrice::where("item_id",$_sa["id"])->where("city_id",$sc_user->shop->city_id)->first();
                    // if($sip){
                    //     $_sa["amount"]=$sip->price;
                    // }
                    $sip=SupplyCityPrice::where("supply_id",$_sa["id"])->where("city_id",$city_id)->where("status",1)->first();
                    if($sip){
                        $_sa["amount"]=$sip->price;
                    }else{
                        $ss_temp=SupplySub::where("supply_id",$_sa["id"])->where("status",1)->first();
                        $_sa["amount"]=isset($ss_temp)?$ss_temp->usual_price:0;
                    }

                    $temp_amount=$_sa["amount"]*$wmcf->supply_coefficient;
                    $temp_amount=Common::upFow($temp_amount);
                    $order_product_item[]=array(
                        "item_id"=>$_sa["id"],
                        "item_name"=>$_sa["name"],
                        "amount"=>$temp_amount,
                        "num"=>$_sa["number"],
                        "type"=>1,
                        "unit"=>$_sa["unit"],
                        "is_adapter"=>$_sa["is_adapter"]
                    );
                    $count_amount+=$temp_amount*$_sa["number"];
                    $total+=$temp_amount*$_sa["number"];
                }
                foreach ($p_item['server'] as $_se) {
                    $res=PricingStrategy::getStringStrategy($csi->id,$_se['id'],$city_id);
                    if($res['code']==0){
                        $temp_amount=$res['price'];
                    }else{
                        //判断工时类型为工时类型还是固定价格
                        if($_se['type'] ==1 ){
                            $temp_amount=$whp->price*$wmcf->work_coefficient*$_se["work_hours"];
                        }elseif($_se['type']==2){
                            $temp_amount=$_se["price"];
                        }
                    }
                    $order_product_item[]=array(
                        "item_id"=>$_se["id"],
                        "item_name"=>$_se["name"],
                        "amount"=>$temp_amount,
                        "num"=>$_se["work_hours"],
                        "type"=>2,
                        "unit"=>"个",
                        "is_adapter"=>1
                    );
                    $count_amount+=$temp_amount;
                    $total+=$temp_amount;
                }

                $opp=new OrderProductAcPackage;
                $opp->order_id=$orders->order_id;
                $opp->order_product_id=$order_product->id;
                $opp->package_id=$pi['package_id'];
                $opp->package_name=$package->name;
                $opp->work_station=0;
                $opp->work_status='prepare';
                $opp->amount=$count_amount;
                $opp->engineer_id=0;
                $opp->save();



                //添加各个产品项
                foreach ($order_product_item as $opi) {
                    $opaci=new OrderProductAcItem;
                    $opaci->order_id=$orders->order_id;
                    $opaci->order_product_package_id=$opp->id;
                    $opaci->item_id=$opi["item_id"];
                    $opaci->item_name=$opi["item_name"];
                    $opaci->amount=$opi["amount"];
                    $opaci->num=$opi["num"];
                    $opaci->type=$opi["type"];
                    $opaci->item_type=0;
                    $opaci->unit=$opi["unit"];
                    $opaci->discount=$pa->discount;
                    $opaci->is_adapter=$opi['is_adapter'];
                    $opaci->discount=1;
                    $opaci->counter_amount=$pa->price;
                    $opaci->save();
                    //判断特殊油品
                    if($opi["type"]==1){
                        if(in_array($opi["item_id"],Config::get("smy.item_id"))){//如果新耗材是特殊油品
                            OrderSmyFilling::saveosf($order->shop_id,$opi["item_id"],"activity",$opaci->id,$order->order_id);
                        }
                    }
                }
                if(empty($pa->price) || $pa->discount == 0){
                    $order_product->discount=$count_amount - ($count_amount * $pa->discount);
                    $order_product->amount=$count_amount;
                    $order_product->counter_amount=$count_amount * $pa->discount;
                }else{
                    $order_product->discount=$pa->price - ($pa->price * $pa->discount);
                    $order_product->amount=$pa->price;
                    $order_product->counter_amount=$pa->price * $pa->discount;
                }
            }
            $order_product->save();
            //订单应付金额及优惠信息将重新计算，废弃原有再优惠信息上加减
            //Orders::calculate($orders->order_id);
            return $response = array('code' => 0, 'msg' => 'ok','result'=>array('order_id'=>Input::get('order_id')));
        });
    }


    /**
     * @Author:      sqq
     * @DateTime:    2016-08-03 11:10:41
     * @Description: 添加 基础产品的 方法
     */
    public static function saveBaseProducts($package_arr,$type,$sc_user_id,$order_id){
        return DB::transaction(function () use ($package_arr,$type,$sc_user_id,$order_id) {
            $order=Orders::where("order_id",$order_id)->lockForUpdate()->first();
            $temp_amount=0;
            Log::info("info", array("system" => "api3.0" ,"event" => "测试基础包", 'input' =>Input::all() ,"output" => $package_arr));
            foreach ($package_arr as $_pa) {
                // $coupon_info=CouponOrderInfo::where("order_id",$order->order_id)
                // ->where("coupon_order_info.status",1)
                // ->where("coupon_base_info.type",2)
                // ->leftJoin('coupon_base_info','coupon_order_info.coupon_base_info_id','=','coupon_base_info.id')
                // ->select('coupon_order_info.coupon_id','coupon_base_info.pack_id')
                // ->first();
                // if($coupon_info->pack_id==$_pa['product_id']){
                //     $res=Orders::changeCouponStatus($order->order_id,4,$coupon_info->coupon_id,$sc_user_id);
                //     if($res['code']>0){
                //         return $res;
                //     }
                // }
                $order_product=new OrderProduct;
                $order_product->order_id=$order_id;
                $order_product->product_type="base";
                $order_product->product_id=$_pa["product_id"];
                $order_product->product_name=$_pa["product_name"];
                $order_product->status=1;
                $order_product->amount=$_pa["amount"];
                $order_product->discount=$_pa['discount_rate'];
                $order_product->counter_amount=$_pa["amount"] * $_pa['discount_rate'];
                $order_product->save();
                //添加各个产品项
                foreach ($_pa["item"] as $_pi) {
                    $opi=new OrderProductBaseItem;
                    $opi->order_id=$order_id;
                    $opi->order_product_package_id=$order_product->id;
                    $opi->item_id=$_pi["item_id"];
                    $opi->item_name=$_pi["item_name"];
                    $opi->amount=$_pi["amount"];
                    $opi->num=$_pi["num"];
                    $opi->type=$_pi["type"];
                    $opi->unit=$_pi["unit"];
                    $opi->is_adapter=$_pi["is_adapter"];
                    $opi->discount=$_pa['discount_rate'];
                    $opi->counter_amount=$_pa["amount"] * $_pa['discount_rate'];
                    $opi->save();
                    //判断特殊油品
                    if($_pi["type"]==1){
                        if(in_array($_pi["item_id"],Config::get("smy.item_id"))){//如果新耗材是特殊油品
                            OrderSmyFilling::saveosf($order->shop_id,$_pi["item_id"],"base",$opi->id,$order->order_id);
                        }
                    }

                }

                //记录添加日志
                $opr=new OrderProductRecord();
                $opr->order_id=$order_id;
                $opr->product_type="base";
                $opr->operation_type='create';
                $opr->num=1;
                $opr->operation_grade='一级';
                $opr->user_id=$sc_user_id;
                $opr->user_type='sc';
                $opr->operation_id=$order_product->id;
                $opr->name=$order_product->product_name;
                $opr->amount=$order_product->amount;
                $opr->save();
            }
            //订单应付金额及优惠信息将重新计算，废弃原有再优惠信息上加减
            //Orders::calculate($order_id);
            return $order_id;
        });
    }


    public static function city_shop($data, $wechat_user){
        $data = json_decode($data,true);
        $temp = [];
        $shops = [];
        $res = [];
        if(!empty($data)){
            foreach($data as $key => $val){
                if($val['service_type'] == 1){
                    //根据服务获取适配门店
                    $temp[] = ProductActivityShop::where('product_activity_id',$val['id'])->distinct()->select('shop_id')->get()->toArray();
                }else{
                    //基础包
                    //获取适配的城市
                    //$city = ProductPackageCity::where('pack_id',$val['id'])->distinct()->where('status',1)->select('city_id')->get()->toArray();
//                    if(!is_null($city)){
//                        $city_id = array_column($city,'city_id');
//                        $temp[] = MaintenanceShops::where('status',1)->where('is_show',1)->whereIn('city_id',$city_id)->select('id as shop_id')->get()->toArray();
//                    }
                    $temp[] = ProductPackageShop::where('pack_id',$val['id'])->distinct()->where('status',1)->select('shop_id')->get()->toArray();

                }
            }

            if(!empty($temp)){
                $tmpArr = $res = $result = $ji_first = $jiaoji = array();
                foreach($temp as $k  => $value){
                    //foreach($value as $ke => $ve){
                        $id = array_column($value,'shop_id');
                        if($k == 0){
                            $tmpArr = $ji_first = $jiaoji = $id;
                        }else{
                            $res = $jiaoji = array_intersect($ji_first, $id);
//                            if(!is_null($jiaoji)){
//                                if(!in_array($value['shop_id'],$tmpArr)){
//                                    print_r($value);die;
//                                    $tmpArr[] = $value['shop_id'];
//                                }else{
//                                    $res[] = $value['shop_id'];
//                                }
//                            }
                        }
                    //}
                }
                if(count($data) == 1){
                    $res = array_unique($tmpArr);
                }else{
                    $res = array_unique($res);
                }

                if(!empty($res)){
                    $shops = MaintenanceShops::where('status',1)->where('is_show',1)->whereIn('id',$res)->select('id','name','image','small_image','longitude','latitude','tel_phone')->get();
                    if(!is_null($shops)){
                        foreach($shops as $v){
                            $order_count = Orders::getToMaintenanceShops(isset($wechat_user->id)?$wechat_user->id:Null,$v->id);
                            if($order_count != 0){
                                $v->to_shop = 1;
                            }else{
                                $v->to_shop = 0;
                            }
                        }
                        foreach($shops as $key=>$s){
                            $shops[$key]['shop_url']=Config::get('config.shop_img_url');
                        }
                    }
                }
            }
        }
        $result = array('id' => $res, 'shops' => $shops);$result = array('id' => $res, 'shops' => $shops);
        return $result;
    }


    /**
     * @Author:          sqq
     * @DateTime:        2018-09-18 上午09:58
     * @Description:     api3.0 服务
     */
    public static function serviceShow($ahp_service, $model, $city_id, $shop_id, $type){
        header("Content-type:text/html;charset=utf-8");
        //$service = [];
        if($ahp_service){
            $ahp_service = $ahp_service->toArray();
            //$ahp = $ahp_service;
            foreach($ahp_service as $key => $val) {
//                $ahp_service[$key]['thumbnail'] = Config::get('config.ahp_upload_files_url').$val['thumbnail'];
//                $ahp_service[$key]['first_picture'] = Config::get('config.ahp_upload_files_url').$val['first_picture'];
                $ahp_service[$key]['url'] = Config::get('config.ahp_upload_files_url');
                //$ahp[$key]['service'] = [];
                //查询ahp服务所包含的cms/mkt服务
                if ($val['service_type'] == 1) {
                    //mkt活动包
                    $ahp_service[$key]['service'] = AhpServiceMktCms::join('product_activity', 'ahp_service_mkt_cms.mkt_cms_id', '=', 'product_activity.id')->where('ahp_service_mkt_cms.service_id', $val['id'])->select('product_activity.id', 'product_activity.name', 'price', 'product_activity.type', 'discount')->get();
                } else {
                    //cms基础包
                    $ahp_service[$key]['service'] = AhpServiceMktCms::join('product_package', 'ahp_service_mkt_cms.mkt_cms_id', '=', 'product_package.id')->where('ahp_service_mkt_cms.service_id', $val['id'])->select('product_package.id', 'product_package.name')->get();
                }
                $ahp_auth_item_id = AhpServiceClassification::getItem($val['id']);
                if($ahp_service[$key]['service']){
                    $ahp_service[$key]['service'] = $ahp_service[$key]['service']->toArray();
                    foreach($ahp_service[$key]['service'] as $s_key => $s_val){
                        //判断是否适配城市、门店
                        if($shop_id && $val['service_type'] == 1){
                            //判断适配门店
                           $product = ProductActivityShop::where('product_activity_id',$s_val['id'])->where('shop_id',$shop_id)->first();
                        }else{
                            //判断适配城市
                            if($val['service_type'] == 1){
                                $table = 'ProductActivityCity';
                                $id = 'product_activity_id';
                                $product = $table::where($id,$s_val['id'])->where('city_id',$city_id)->where('status',1)->first();
                            }else{
                                $table = 'ProductPackageShop';
                                $id = 'pack_id';
                                if($shop_id){
                                    $product = $table::where($id,$s_val['id'])->where('shop_id',$shop_id)->where('status',1)->first();
                                }else{
                                    $product = $table::where($id,$s_val['id'])->where('city_id',$city_id)->where('status',1)->first();
                                }
                            }
                        }
                        if(is_null($product)){
                            if (isset($ahp_service[$key]['service'][$s_key])) {
                                unset($ahp_service[$key]['service'][$s_key]);
                                continue;
                            }
                        }

                        //查询服务包适配的所有服务
                        $mkt_cms = array($s_val['id']);
                        if ($mkt_cms) {
                            if ($val['service_type'] == 1) { //mkt
                                $pap = ProductActivityPackage::leftJoin("product_package", "product_activity_package.package_id", "=", "product_package.id")
                                    ->whereIn('product_activity_id', $mkt_cms)
                                    ->distinct('package_id')
                                    ->groupBy('package_id')
                                    ->select('package_id', 'product_activity_package.id', 'product_package.name')->get();
                                if (!empty($pap)) {
                                    $pap = $pap->toArray();
                                    $package_id = array_column($pap, 'package_id');
                                    $product_pack_item = ProductPackItem::leftjoin("supply_classification", "supply_classification.id", "=", "product_pack_item.item_id")
                                        ->whereIn("pack_id", $package_id)
                                        ->where("type", 3)
                                        ->where("product_pack_item.status", 1)
                                        ->distinct('product_pack_item.item_id')
                                        ->select("supply_classification.name", "product_pack_item.item_id")
                                        ->get();
                                } else {
                                    $product_pack_item = '';
                                }
                            } else {
                                //cms
                                $product_pack_item = ProductPackItem::leftjoin("supply_classification", "supply_classification.id", "=", "product_pack_item.item_id")
                                    ->whereIn("pack_id", $mkt_cms)
                                    ->where("type", 3)
                                    ->where("product_pack_item.status", 1)
                                    ->distinct('product_pack_item.item_id')
                                    ->select("supply_classification.name", "product_pack_item.item_id")
                                    ->groupBy("product_pack_item.item_id")
                                    ->get();

                            }
                        } else {
                            $product_pack_item = '';
                        }
                        //判断服务中的耗材是否有推荐，没有就删除
//判断是基础包还是活动包
                        if(!is_null($product_pack_item)){
                            $product_pack_item = $product_pack_item->toArray();
                        }
                        $ahp_service[$key]['service'][$s_key]['product_pack_item'] = $product_pack_item;

                        $arr = [];
                        $item_id = [];
                        if ($ahp_service[$key]['service'][$s_key]['product_pack_item']) {
                            foreach ($ahp_service[$key]['service'][$s_key]['product_pack_item'] as $_ids => $_id) {
                                if (!is_null($ahp_auth_item_id)){
                                    foreach ($ahp_auth_item_id as $_itme_id) {
                                        if (((int)$_itme_id->item_id == (int)$_id['item_id'])) {
                                            $arr = DataSupplyProductItem::getDefaultC($model, $_id['item_id']);
                                            if (is_null($arr)) {
                                                if (isset($ahp_service[$key]['service'][$s_key])) {
                                                    unset($ahp_service[$key]['service'][$s_key]);
                                                    continue;
                                                }
                                            }
//                                            if(!is_null($arr) && !is_null($product)){
//                                                if(!array_key_exists($ahp[$key]['id'],$service)){
//                                                    array_push($ahp[$key]['service'], $ahp_service[$key]['service'][$s_key]);
//                                                }
//                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    //$service[$ahp[$key]['id']] = $ahp[$key];
                }else{
                    unset($ahp_service[$key]);
                }
            }
        }

        if($ahp_service){
            foreach($ahp_service as $k => $v){
                foreach($v['service'] as $_k => $_v){
                    if($_v['product_pack_item']){
                        foreach($_v['product_pack_item'] as $pack => $item){
                            $a = AhpService::getDefaultC($model, $item['item_id']);
                            if($a){
                                $ahp_service[$k]['service'][$_k]['product_pack_item'][$pack]['item'][] = $a;
                            }
                        }
                    }
                }
            }
        }

        return $ahp_service;
    }



    public static function serviceShowNew($ahp_service, $model, $city_id, $shop_id, $type){
        header("Content-type:text/html;charset=utf-8");
        //$service = [];
        if($ahp_service) {
            $ahp_service = $ahp_service->toArray();
            //$ahp = $ahp_service;
            $asm_hd_ararry = $asm_jc_ararry = array();
            foreach ($ahp_service as $key => $val) {
                //$ahp_service[$key]['ahp_auth_item_id'] = array_column(AhpServiceClassification::getItem($val['id'])->toArray(),'item_id');
                $ahp_service[$key]['ahp_auth_item_id'] = AhpServiceClassification::getItem($val['id'])->toArray();
                $ahp_service[$key]['url'] = Config::get('config.ahp_upload_files_url');
                if ($val['service_type'] == 1) {
                    //mkt活动包
                    $asm_hd_ararry[] = $val['id'];
                } else {
                    //cms基础包
                    $asm_jc_ararry[] = $val['id'];
                }
            }

            $temp_hd_sevice = AhpServiceMktCms::join('product_activity', 'ahp_service_mkt_cms.mkt_cms_id', '=', 'product_activity.id')->whereIn('ahp_service_mkt_cms.service_id', $asm_hd_ararry)->select('product_activity.id', 'product_activity.name', 'price', 'product_activity.type', 'discount', 'service_id')->get();
            $temp_hd_sevice_ids = array();
            $temp_hd_sevice_array = array();
            foreach ($temp_hd_sevice as $_ths) {
                $temp_hd_sevice_ids[$_ths->service_id][] = $_ths->id;
                $temp_hd_sevice_array[$_ths->service_id][] = $_ths;
            }

            $temp_jc_sevice = AhpServiceMktCms::join('product_package', 'ahp_service_mkt_cms.mkt_cms_id', '=', 'product_package.id')->whereIn('ahp_service_mkt_cms.service_id', $asm_jc_ararry)->select('product_package.id', 'product_package.name', 'service_id')->get();
            $temp_jc_sevice_ids = array();
            $temp_jc_sevice_array = array();
            if(isset($temp_jc_sevice) && !is_null($temp_jc_sevice)){
                foreach ($temp_jc_sevice as $_tjs) {
                    $temp_jc_sevice_ids[$_tjs->id][] = $_tjs->id;
                    $temp_jc_sevice_array[$_tjs->service_id][] = $_tjs;
                }
            }

            $hd_ids = [];
            if (sizeof($temp_hd_sevice_ids) > 0) {
                foreach ($temp_hd_sevice_ids as $hd_id) {
                    $hd_ids += $hd_id;
                }
            }

            //基础包数据处理
            $jc_ids = [];
            if (sizeof($temp_jc_sevice_ids) > 0) {
                foreach ($temp_jc_sevice_ids as $jc_id) {
                    $jc_ids[] = $jc_id[0];
                }
            }

            if ($shop_id && sizeof($temp_hd_sevice_ids) > 0 && $hd_ids) {
                //判断适配门店
                $hd_product = ProductActivityShop::whereIn('product_activity_id', $hd_ids)->where('shop_id', $shop_id)->select("product_activity_id")->get();
            } else {
                $hd_product = ProductActivityCity::whereIn("product_activity_id", $hd_ids)->where('city_id', $city_id)->where('status', 1)->select("product_activity_id")->get();
            }

            if($shop_id && sizeof($temp_jc_sevice_ids) > 0 && $jc_ids){
                $jc_product = ProductPackageShop::whereIn("pack_id", $jc_ids)->where('shop_id', $shop_id)->where('status', 1)->select("pack_id")->get();
            }else{
                $jc_product = ProductPackageShop::whereIn("pack_id", $jc_ids)->where('city_id', $city_id)->where('status', 1)->select("pack_id")->get();
            }

            $worktime_hd = array();
            if (isset($hd_product)) { //mkt
                $hd_product = $hd_product->toArray();
                $hd_product = array_column($hd_product, 'product_activity_id');
                $pap = ProductActivityPackage::leftJoin("product_package", "product_activity_package.package_id", "=", "product_package.id")
                    ->whereIn('product_activity_id', $hd_product)
                    //->distinct('package_id')
                    //->groupBy('package_id')
                    ->select('package_id', 'product_activity_id', 'product_activity_package.id', 'product_package.name')->get();
                if (!empty($pap)) {
                    $pap = $pap->toArray();
                    $package_id = array_column($pap, 'package_id');
                    $product_pack_hd = ProductPackItem::leftjoin("supply_classification", "supply_classification.id", "=", "product_pack_item.item_id")
                        ->whereIn("pack_id", $package_id)
                        ->where("type", 3)
                        ->where("product_pack_item.status", 1)
                        //->distinct('product_pack_item.item_id')
                        ->select("supply_classification.name", "product_pack_item.item_id", "pack_id")
                        ->get();
                    $worktime_hd = ProductPackItem::leftjoin("supply_classification", "supply_classification.id", "=", "product_pack_item.item_id")
                        ->whereIn("pack_id", $package_id)
                        ->where("type", 2)
                        ->where("product_pack_item.status", 1)
                        ->select("supply_classification.name", "product_pack_item.item_id", "pack_id")
                        ->get()->toArray();
                } else {
                    $product_pack_hd = array();
                    $worktime_hd = array();
                }
            }
            $worktime_count_hd = array();
            if(!is_null($worktime_hd)){
                $worktime_count_hd = array_count_values(array_column($worktime_hd,'pack_id'));
            }

            $jcid = [];
            $worktime_jc = array();
            $product_pack_jc = array();
            if (isset($jc_product)) {
                //cms
                $jc_product = $jc_product->toArray();
                $jcid = array_column($jc_product, 'pack_id');
                $product_pack_jc = ProductPackItem::leftjoin("supply_classification", "supply_classification.id", "=", "product_pack_item.item_id")
                    ->whereIn("pack_id", $jcid)
                    ->where("type", 3)
                    ->where("product_pack_item.status", 1)
                    //->distinct('product_pack_item.item_id')
                    ->select("supply_classification.name", "product_pack_item.item_id", "pack_id")
                    //->groupBy("product_pack_item.item_id")
                    ->get();
                $worktime_jc = ProductPackItem::leftjoin("supply_classification", "supply_classification.id", "=", "product_pack_item.item_id")
                    ->whereIn("pack_id", $jcid)
                    ->where("type", 2)
                    ->where("product_pack_item.status", 1)
                    ->select("supply_classification.name", "product_pack_item.item_id", "pack_id")
                    ->get()->toArray();
            }
            $worktime_count_jc = array();
            if(!is_null($worktime_jc)){
                $worktime_count_jc = array_count_values(array_column($worktime_jc,'pack_id'));
            }

            //查询耗材
            if (isset($product_pack_hd) && !is_null($product_pack_hd)) {
                $product_pack_hd = json_decode(json_encode($product_pack_hd), true);
                $item_fenlei_id = array_column($product_pack_hd, 'item_id'); //耗材分类id
                $hd_item = array();
                foreach ($item_fenlei_id as $item) {
                    $a = AhpService::getDefault($model, $item);
                    //$a = ProductPackage::getItemCNewS5($model, $item);
                    $hd_item[$item][] = $a;
                }
            }
            if (isset($product_pack_jc) && !is_null($product_pack_jc)) {
                $product_pack_jc = json_decode(json_encode($product_pack_jc), true);
                $jc_item_fenlei_id = array_column($product_pack_jc, 'item_id'); //耗材分类id
                $jc_item = array();
                foreach ($jc_item_fenlei_id as $item1) {
                    $a = AhpService::getDefault($model, $item1);
                    //$a = ProductPackage::getItemCNewS5($model, $item1);
                    $jc_item[$item1][] = $a;
                }
            }

            //组装数据
            //组装活动包下面的基础包数据
            if (isset($pap) && !is_null($pap)) {
                $pap = json_decode(json_encode($pap), true);
                $hd_jc_item = array();
                $hd_jc_item_array = array();
                foreach ($pap as $_hd_js) {
                    $hd_jc_item[$_hd_js['product_activity_id']] = $_hd_js['package_id'];
                    $hd_jc_item_array[$_hd_js['product_activity_id']][] = $_hd_js;
                }
            }

            //处理耗材分类
            if (isset($product_pack_hd) && !is_null($product_pack_hd)) {
                $pack_hd = array();
                foreach ($product_pack_hd as $pack) {
                    $pack_hd[$pack['pack_id']][] = $pack;
                }
            }
            if (isset($product_pack_jc) && !is_null($product_pack_jc)) {
                $pack_jc = array();
                foreach ($product_pack_jc as $pack1) {
                    $pack_jc[$pack1['pack_id']][] = $pack1;
                }
            }

            //首先在ahp配置的包里面添加对应的基础包/活动包
            //活动包
            foreach ($ahp_service as $_key => $_val) {
                $tmp = array();
                if (isset($temp_hd_sevice_array[$_val['id']])) {
                    foreach ($temp_hd_sevice_array[$_val['id']] as $hd_k => $hd_v) {
                        $hd_v = json_decode(json_encode($hd_v), true);
                        if (in_array($hd_v['id'], $hd_product)) {
                            $tmp[] = $hd_v;
                        }
                    }
                    $ahp_service[$_key]['service'] = $tmp;
                    //处理基础包
                    $temp_jc = array();
                    if(isset($ahp_service[$_key]['service'])){
                        foreach ($ahp_service[$_key]['service'] as $_hd_bao_k => $_hd_bao_v) {
                            $ahp_service[$_key]['service'][$_hd_bao_k]['jc'] = array();
                            if (isset($hd_jc_item_array[$_hd_bao_v['id']])) {
                                $ahp_service[$_key]['service'][$_hd_bao_k]['jc'] = $hd_jc_item_array[$_hd_bao_v['id']];
                            }

                            //处理耗材分类
                            if(isset($ahp_service[$_key]['service'][$_hd_bao_k]['jc'])){

                                foreach ($ahp_service[$_key]['service'][$_hd_bao_k]['jc'] as $_js_k => $_js_v) {

                                    if(!empty($worktime_count_hd)){
                                        $ahp_service[$_key]['service'][$_hd_bao_k]['jc'][$_js_k]['worktime'] = isset($worktime_count_hd[$_js_v['package_id']]) ? $worktime_count_hd[$_js_v['package_id']] : 0;
                                    }else{
                                        $ahp_service[$_key]['service'][$_hd_bao_k]['jc'][$_js_k]['worktime'] = 0;
                                    }

                                    if (isset($pack_hd[$_js_v['package_id']])) {
                                        $ahp_service[$_key]['service'][$_hd_bao_k]['jc'][$_js_k]['product_pack_item'] = $pack_hd[$_js_v['package_id']];
                                    }

                                    //处理耗材
                                    foreach ($ahp_service[$_key]['service'][$_hd_bao_k]['jc'][$_js_k]['product_pack_item'] as $pack_item_k => $pack_item_v) {
                                        $ahp_service[$_key]['service'][$_hd_bao_k]['jc'][$_js_k]['product_pack_item'][$pack_item_k]['item'] = array();
                                        if (isset($hd_item[$pack_item_v['item_id']])) {
                                            $hd_item[$pack_item_v['item_id']] = json_decode(json_encode($hd_item[$pack_item_v['item_id']]), true);
                                            if(isset($hd_item[$pack_item_v['item_id']]) && !empty($hd_item[$pack_item_v['item_id']][0])){
                                                $ahp_service[$_key]['service'][$_hd_bao_k]['jc'][$_js_k]['product_pack_item'][$pack_item_k]['item'] = $hd_item[$pack_item_v['item_id']];
                                            }
                                        }
                                    }

                                    if(isset($ahp_service[$_key]['ahp_auth_item_id'])){
                                        foreach($ahp_service[$_key]['ahp_auth_item_id'] as $_ahp_auth_k => $_ahp_auth_v){
                                            //$temp_item = array();
                                            if(isset($ahp_service[$_key]['service'][$_hd_bao_k]['jc'][$_js_k]['product_pack_item'])){
                                                foreach($ahp_service[$_key]['service'][$_hd_bao_k]['jc'][$_js_k]['product_pack_item'] as $_pack_item_k => $_pack_item_v) {
                                                    if($_ahp_auth_v['item_id'] == $_pack_item_v['item_id']){
                                                        if(empty($_pack_item_v['item']) && $ahp_service[$_key]['service'][$_hd_bao_k]['jc'][$_js_k]['worktime'] == 0){
                                                            //$temp_item[] = $ahp_service[$_key]['service'][$_hd_bao_k]['jc'][$_js_k];
                                                            unset($ahp_service[$_key]['service'][$_hd_bao_k]['jc'][$_js_k]);
                                                        }else{
                                                            foreach($_pack_item_v['item'] as $_k1 => $_v1){
                                                                $ahp_service[$_key]['service'][$_hd_bao_k]['jc'][$_js_k]['product_pack_item'][$_pack_item_k]['item'][$_k1]['mkt_item_id'] = '';
                                                                $pap=ProductActivityPackage::leftJoin("product_package","product_activity_package.package_id","=","product_package.id")
                                                                    ->where('product_activity_id',$ahp_service[$_key]['service'][$_hd_bao_k]['id'])
                                                                    ->select('package_id','product_activity_package.id','product_package.name')->get();
                                                                if (!empty($pap)){
                                                                    foreach ($pap as $value){
                                                                        $product_pack_item = ProductPackItem::leftjoin("supply_classification","supply_classification.id","=","product_pack_item.item_id" )
                                                                            ->where("pack_id",$value->package_id)
                                                                            ->where("type",3)
                                                                            ->where("product_pack_item.status",1)
                                                                            ->select("supply_classification.name","product_pack_item.item_id")
                                                                            ->get();

                                                                        foreach ($product_pack_item as $v2) {
                                                                            $papi = ProductActivityPackageItem::where("subject",$v2->item_id)
                                                                                ->where("product_activity_package_item.product_activity_package_id",$value->id)
                                                                                ->select("id","supply_item_id","number")
                                                                                ->first();

                                                                            $v2->papi = $papi;
                                                                            if (!empty($papi->supply_item_id)) {
                                                                                $v2->item = SupplyItem::getCacheItemInfo($papi->supply_item_id);
                                                                                $v2->item['mkt_item_id'] = $v2->item['id'];
                                                                                $ahp_service[$_key]['service'][$_hd_bao_k]['jc'][$_js_k]['product_pack_item'][$_pack_item_k]['item'][$_k1] = $v2->item;


         $s = $v2->item['subject'];
         $mkt_item_id = $hd_item[$s][0]['id'];
                                                                                $ahp_service[$_key]['service'][$_hd_bao_k]['jc'][$_js_k]['product_pack_item'][$_pack_item_k]['item'][$_k1]['mkt_item_id'] = $mkt_item_id;
                                                                                $ahp_service[$_key]['service'][$_hd_bao_k]['jc'][$_js_k]['product_pack_item'][$_pack_item_k]['item'][$_k1]['isdefualt'] = '0';
                                                                                $sd = DataSupplyProductItem::where('car_model_id',$model)->where('city',$city_id)->where('status',1)->where('supply_item_id',$papi->supply_item_id)->first();
                                                                                if(!is_null($sd)){
                                                                                    $ahp_service[$_key]['service'][$_hd_bao_k]['jc'][$_js_k]['product_pack_item'][$_pack_item_k]['item'][$_k1]['isdefualt'] = $sd->isdefualt;
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                                //$ahp_service[$_key]['service'][$_hd_bao_k]['jc'] = $temp_item;
                                            }
                                        }
                                    }
                                }
                            }
                            if(empty($ahp_service[$_key]['service'][$_hd_bao_k]['jc'])){
                                // $temp_jc[] = $ahp_service[$_key]['service'][$_hd_bao_k];
                                unset($ahp_service[$_key]['service'][$_hd_bao_k]);
                            }
                        }
                    }
                    //$ahp_service[$_key]['service'] = $temp_jc;
                }
            }
            //die;

            //基础包
            foreach ($ahp_service as $_key => &$_val) {
                $tmp = array();
                if (isset($temp_jc_sevice_array[$_val['id']])) {
                    foreach ($temp_jc_sevice_array[$_val['id']] as $jc_k => $jc_v) {
                        $jc_v = json_decode(json_encode($jc_v), true);
                        if (in_array($jc_v['id'], $jcid)) {
                            $tmp[] = $jc_v;
                        }
                    }
                    $ahp_service[$_key]['service'] = $tmp;
                    //处理耗材分类
                    if(isset($ahp_service[$_key]['service']) && !empty($ahp_service[$_key]['service'])){
                        foreach ($ahp_service[$_key]['service'] as $_js_k => $_js_v) {
                            //foreach($_js_v as $item){
                            $ahp_service[$_key]['service'][$_js_k]['product_pack_item'] = [];
                            if (isset($pack_jc[$_js_v['id']])) {
                                $ahp_service[$_key]['service'][$_js_k]['product_pack_item'] = $pack_jc[$_js_v['id']];
                            }
                            //}

                            if(!empty($worktime_count_jc)){
                                $ahp_service[$_key]['service'][$_js_k]['worktime'] = isset($worktime_count_jc[$_js_v['id']]) ? $worktime_count_jc[$_js_v['id']] : 0;
                            }else{
                                $ahp_service[$_key]['service'][$_js_k]['worktime'] = 0;
                            }

                            //处理耗材
//                            if ((!empty($ahp_service[$_key]['service'][$_js_k]['product_pack_item']) && $ahp_service[$_key]['service'][$_js_k]['worktime'] != 0) || (empty($ahp_service[$_key]['service'][$_js_k]['product_pack_item']) && $ahp_service[$_key]['service'][$_js_k]['worktime'] != 0) || ($ahp_service[$_key]['service'][$_js_k]['worktime'] == 0 && !empty($ahp_service[$_key]['service'][$_js_k]['product_pack_item']))) {
//                                foreach ($ahp_service[$_key]['service'][$_js_k]['product_pack_item'] as $pack_item_k => $pack_item_v) {
//                                    $ahp_service[$_key]['service'][$_js_k]['product_pack_item'][$pack_item_k]['item'] = array();
//                                    if (isset($jc_item[$pack_item_v['item_id']])) {
//                                        $jc_item[$pack_item_v['item_id']] = json_decode(json_encode($jc_item[$pack_item_v['item_id']]), true);
//                                        if(isset($jc_item[$pack_item_v['item_id']]) && $jc_item[$pack_item_v['item_id']]){
//                                            if(isset( $jc_item[$pack_item_v['item_id']][0])){
//                                                $ahp_service[$_key]['service'][$_js_k]['product_pack_item'][$pack_item_k]['item'] = $jc_item[$pack_item_v['item_id']];
//                                            }
//                                        }
//                                    }
//                                }
//                            }else{
//                                unset($ahp_service[$_key]['service'][$_js_k]);
//                            }

                            if ((empty($ahp_service[$_key]['service'][$_js_k]['product_pack_item']) && $ahp_service[$_key]['service'][$_js_k]['worktime'] == 0)) {
                                unset($ahp_service[$_key]['service'][$_js_k]);
                            }else{
                                foreach ($ahp_service[$_key]['service'][$_js_k]['product_pack_item'] as $pack_item_k => $pack_item_v) {
                                    $ahp_service[$_key]['service'][$_js_k]['product_pack_item'][$pack_item_k]['item'] = array();
                                    if (isset($jc_item[$pack_item_v['item_id']])) {
                                        $jc_item[$pack_item_v['item_id']] = json_decode(json_encode($jc_item[$pack_item_v['item_id']]), true);
                                        if(isset($jc_item[$pack_item_v['item_id']]) && $jc_item[$pack_item_v['item_id']]){
                                            if(isset( $jc_item[$pack_item_v['item_id']][0])){
                                                $ahp_service[$_key]['service'][$_js_k]['product_pack_item'][$pack_item_k]['item'] = $jc_item[$pack_item_v['item_id']];
                                            }
                                        }
                                    }
                                }
                            }

                            $temp_item = array();
                            if(isset($ahp_service[$_key]['ahp_auth_item_id'])){
                                foreach($ahp_service[$_key]['ahp_auth_item_id'] as $_ahp_auth_k => $_ahp_auth_v){
                                    if(isset($ahp_service[$_key]['service'][$_js_k]['product_pack_item'])){
                                        foreach($ahp_service[$_key]['service'][$_js_k]['product_pack_item'] as $_pack_item_k => $_pack_item_v){
                                            if(($_ahp_auth_v['item_id'] == $_pack_item_v['item_id'])){
                                                if(empty($_pack_item_v['item'])){
                                                    //$temp_item[] = $ahp_service[$_key]['service'][$_js_k];
                                                    unset($ahp_service[$_key]['service'][$_js_k]);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            //$ahp_service[$_key]['service'] = $temp_item;
                        }
                    }
                }
            }
        }
        //print_r($ahp_service);die;

        if($ahp_service && $type == 2) { //详情
            foreach ($ahp_service as $k => $v) {
                $isfefualt = AhpServiceIsdefualt::where('service_id',$v['id'])->first();

                //if(!is_null($isfefualt)){
                    $id = [];
                    $id1 = [];
                    $id2 = [];
                    $id3 = [];
                    $ARR = [];
                    $ARR1 = [];
                    $ARR2 = [];
                    $ARR3 = [];
                    $mkt_item_del = array();
                    if(isset($v['service']) && !empty($v['service'])){
                        foreach ($v['service'] as $_k => $_v) {

                            $ahp_service[$k]['service'][$_k]['pap'] = [];

                            $pap=ProductActivityPackage::leftJoin("product_package","product_activity_package.package_id","=","product_package.id")
                                ->where('product_activity_id',$_v['id'])
                                ->select('package_id','product_activity_package.id','product_package.name')->get();

                            if (!empty($pap)){
                                foreach ($pap as $value){
                                    $product_pack_items = ProductPackItem::leftjoin("supply_classification","supply_classification.id","=","product_pack_item.item_id" )
                                        ->where("pack_id",$value->package_id)
                                        ->where("type",3)
                                        ->where("product_pack_item.status",1)
                                        ->select("supply_classification.name","product_pack_item.item_id")
                                        ->get();
                                    foreach ($product_pack_items as $v) {
                                        $papi = ProductActivityPackageItem::where("subject",$v->item_id)
                                            ->where("product_activity_package_item.product_activity_package_id",$value->id)
                                            ->select("id","supply_item_id","number")
                                            ->first();

                                        $v->papi = $papi;
                                        if (!empty($papi->supply_item_id)) {
                                            $v->item = SupplyItem::getCacheItemInfo($papi->supply_item_id);
                                        }
                                    }
                                    $ahp_service[$k]['service'][$_k]['pap'] = $product_pack_items;
                                    $_v['pap'] = $product_pack_items;
                                }
                                //$value->id => product_activity_package_id
                            }



                            if(isset($_v['jc'])){
                                foreach($_v['jc'] as $_jc_k => $_jc_v){
                                    if (!empty($_jc_v['product_pack_item'])) {
                                        foreach ($_jc_v['product_pack_item'] as $pack => $item) {
//                                    echo $item['item_id'].'|'. $isfefualt->item_id ;
//                                    print_r($item['item']).'<br/>';
                                            if (!empty($item['item'])) {
                                                foreach ($item['item'] as $pack_item => $packs) {

                                                    foreach($_v['pap'] as $_p_k =>$_p_v){
                                                        if($_p_v['item_id'] == $item['item_id'] && isset($_p_v['item'])){
                                                            //判断是否适配当前车型
                                                            $model_item = SupplyProductItem::where('car_model_id',$model)->where('supply_item_id',$_p_v['item']['id'])->where('status',1)->first();
                                                            if(is_null($model_item)){
                                                                if(isset($_v)){
                                                                    if(!in_array($_v['id'], $mkt_item_del)){
                                                                        array_push($mkt_item_del,$_v['id']);
                                                                    }
                                                                    //unset($_v);
                                                                    //unset($ahp_service[$k]['service'][$_k]);
                                                                }
                                                            }
                                                        }
                                                    }


                                                    if(isset($_v)){
                                                        if (isset($isfefualt->item_id) && $isfefualt->item_id == $item['item_id'] && isset($packs['mkt_item_id']) && $packs['mkt_item_id'] == $packs['id']) {
                                                            if (!in_array($_v['id'], $id)) {
                                                                if(in_array($_v['id'], $id1)){
                                                                    foreach($ARR1 as $arr1_key => $arr1_val){
                                                                        if($_v['id'] == $arr1_val['id']){
                                                                            unset($ARR2[$arr1_key]);break;
                                                                        }
                                                                    }
                                                                    // $qu_key1 = array_search($_v['id'],$ARR1);
                                                                    $id_key1 = array_search($_v['id'],$id1);
                                                                    // unset($ARR1[$qu_key1]);
                                                                    unset($id1[$id_key1]);
                                                                }
                                                                if(in_array($_v['id'], $id2)){
                                                                    foreach($ARR2 as $arr2_key => $arr2_val){
                                                                        if($_v['id'] == $arr2_val['id']){
                                                                            unset($ARR2[$arr2_key]);break;
                                                                        }
                                                                    }
                                                                    $qu_key2 = array_search($_v['id'],$ARR2);
                                                                    $id_key2 = array_search($_v['id'],$id2);
                                                                    unset($id2[$id_key2]);
                                                                }
                                                                array_push($id, $_v['id']);
                                                                array_push($ARR, $_v);
                                                            }
                                                        }else{
                                                            if (isset($packs['isdefualt']) == 1 && isset($isfefualt->item_id) && $isfefualt->item_id == $item['item_id']) {
                                                                if (!in_array($_v['id'], $id1)) {
                                                                    if(in_array($_v['id'], $id2)){
                                                                        foreach($ARR2 as $arr2_key => $arr2_val){
                                                                            if($_v['id'] == $arr2_val['id']){
                                                                                unset($ARR2[$arr2_key]);break;
                                                                            }
                                                                        }
                                                                        $qu_key2 = array_search($_v['id'],$ARR2);
                                                                        $id_key2 = array_search($_v['id'],$id2);
                                                                        unset($id2[$id_key2]);
                                                                    }
                                                                    array_push($id1, $_v['id']);
                                                                    array_push($ARR1, $_v);
                                                                }
                                                            }else{
                                                                if (!in_array($_v['id'], $id) && !in_array($_v['id'], $id1) && !in_array($_v['id'], $id2)){
                                                                    array_push($id2, $_v['id']);
                                                                    array_push($ARR2, $_v);
                                                                }
                                                            }
                                                        }
                                                    }
                                                    // }
                                                }
                                            }
                                        }
                                    }
                                }
                            }else{
                                if (!empty($ahp_service[$k]['service'][$_k]['product_pack_item'])) {
                                    foreach ($ahp_service[$k]['service'][$_k]['product_pack_item'] as $pack => $item) {
//                                    echo $item['item_id'].'|'. $isfefualt->item_id ;
//                                    print_r($item['item']).'<br/>';
                                        if (!empty($item['item'])) {
                                            foreach ($item['item'] as $pack_item => $packs) {
                                                //if ($pack['isdefualt'] != null) {
//                                                //print_r($pack);
//                                                if ($packs['isdefualt'] == 1 && isset($isfefualt->item_id) && $isfefualt->item_id == $item['item_id']) {
//                                                    if (!in_array($_v['id'], $id)) {
//                                                        array_push($id, $_v['id']);
//                                                        array_push($ARR, $_v);
//                                                    }
//                                                }else{
//                                                    if (!in_array($_v['id'], $id1) || !in_array($_v['id'], $id)) {
//                                                        array_push($id1, $_v['id']);
//                                                        array_push($ARR1, $_v);
//                                                    }
//                                                }
                                                if (isset($isfefualt->item_id) && $isfefualt->item_id == $item['item_id'] && isset($packs['mkt_item_id']) && $packs['mkt_item_id'] == isset($packs['id'])) {
                                                    if (!in_array($_v['id'], $id)) {
                                                        if(in_array($_v['id'], $id1)){
                                                            foreach($ARR1 as $arr1_key => $arr1_val){
                                                                if($_v['id'] == $arr1_val['id']){
                                                                    unset($ARR2[$arr1_key]);break;
                                                                }
                                                            }
                                                            // $qu_key1 = array_search($_v['id'],$ARR1);
                                                            $id_key1 = array_search($_v['id'],$id1);
                                                            // unset($ARR1[$qu_key1]);
                                                            unset($id1[$id_key1]);
                                                        }
                                                        if(in_array($_v['id'], $id2)){
                                                            foreach($ARR2 as $arr2_key => $arr2_val){
                                                                if($_v['id'] == $arr2_val['id']){
                                                                    unset($ARR2[$arr2_key]);break;
                                                                }
                                                            }
                                                            $qu_key2 = array_search($_v['id'],$ARR2);
                                                            $id_key2 = array_search($_v['id'],$id2);
                                                            unset($id2[$id_key2]);
                                                        }
                                                        array_push($id, $_v['id']);
                                                        array_push($ARR, $_v);
                                                    }
                                                }else{
                                                    if (isset($packs['isfefualt']) == 1 && isset($isfefualt->item_id) && $isfefualt->item_id == $item['item_id']) {
                                                        if (!in_array($_v['id'], $id1)) {
                                                            if(in_array($_v['id'], $id2)){
                                                                foreach($ARR2 as $arr2_key => $arr2_val){
                                                                    if($_v['id'] == $arr2_val['id']){
                                                                        unset($ARR2[$arr2_key]);break;
                                                                    }
                                                                }
                                                                $qu_key2 = array_search($_v['id'],$ARR2);
                                                                $id_key2 = array_search($_v['id'],$id2);
                                                                unset($id2[$id_key2]);
                                                            }
                                                            array_push($id1, $_v['id']);
                                                            array_push($ARR1, $_v);
                                                        }
                                                    }else{
                                                        if (!in_array($_v['id'], $id) && !in_array($_v['id'], $id1) && !in_array($_v['id'], $id2)){
                                                            array_push($id2, $_v['id']);
                                                            array_push($ARR2, $_v);
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }else{
                                    if(!in_array($_v['id'], $id1) || !in_array($_v['id'], $id)){
                                        array_push($id3, $_v['id']);
                                        array_push($ARR3, $_v);
                                    }
                                }
                            }

                        }
                    }
                    if ($id) {
                        $id = array_values($id);
                        $ARR = array_values($ARR);
                        array_multisort($id, SORT_ASC, $ARR);
                    }
                    if ($id1) {
                        $id1 = array_values($id1);
                        $ARR1 = array_values($ARR1);
                        array_multisort($id1, SORT_ASC, $ARR1);
                    }
                    if ($id2) {
                        $id2 = array_values($id2);
                        $ARR2 = array_values($ARR2);
//                        var_dump($id2);
//                        var_dump($ARR2);
                        //array_multisort($id2, SORT_ASC, $ARR2);
                    }
                if ($id3) {
                    $id3 = array_values($id3);
                    $ARR3 = array_values($ARR3);
                    array_multisort($id3, SORT_ASC, $ARR3);
                }
                    $ahp_service[$k]['service'] =  array_merge($ARR, $ARR1, $ARR2, $ARR3);
                //}
            }


            if(!empty($mkt_item_del)){
                foreach($mkt_item_del as $mkt_key){
                    foreach($ahp_service as $ahp_k1 => $ahp_v1){
                        foreach($ahp_v1['service'] as $ahp_k2 => $ahp_v2){
                            if($mkt_key == $ahp_v2['id']){
                                //var_dump($mkt_key);
                                //var_dump($ahp_v2['id']);
                                unset($ahp_service[$ahp_k1]['service'][$ahp_k2]);
                            }
                        }
                    }
                }
            }
        }
        //die;
        return $ahp_service;
    }


    /**
     * 根据车型id和耗材分类id 获取 一条默认的耗材信息【cms的耗材信息 也既时anhui库中的耗材信息】
     * @param int $car_model_id     车型id
     * @param int $supply_subject   耗材分类id  [anhui库中]
     * @return array | null
     */
    public static function getDefault($car_model_id,$supply_subject){
        // $arr= DB::select("SELECT spi.number,spi.max_number,si.*,ss.`name`,sb.brand_name,ssb.name as ssb_name FROM `supply_product_item` spi
        //     LEFT JOIN supply_item si on si.id=spi.supply_item_id
        //     LEFT JOIN supply_brand sb on si.brand_id=sb.id
        //     LEFT JOIN supply_series ss on ss.id=si.series_id
        //     LEFT JOIN supply_subject ssb on ssb.id=si.`subject`
        //     where
        //     and spi.car_model_id=? and `subject`=? order by spi.isdefualt desc ",array($car_model_id,$supply_subject));

        // $csi=DataCarModel::find($car_model_id);
        //  $tuijian=SupplyRecommend::getRecommendByCarModel($csi);

        $dmcc=DataCarMaintenanceClassificationConfig::where("car_model_id",$car_model_id)->where("supply_classification_id",$supply_subject)->where("status",1)->first();
        // DB::select("select si.id
        // 		from 	  anhui_data.supply_item as si
        // 		LEFT JOIN anhui_data.supply_brand sb on si.brand_id=sb.id
        //     	LEFT JOIN anhui_data.supply_series ss on ss.id=si.series_id
        //         Left Join anhui_data.supply_product_item spi on spi.supply_item_id=si.id
        //         where spi.car_model_id=? and spi.status=1 and si.status=1 and  sb.status=1 and ss.status=1 and si.supply_classification_id=?");
        $temp_dsi=SupplyItem::
        leftJoin("anhui_data.supply_brand","supply_brand.id","=","supply_item.brand_id")->
        leftJoin("anhui_data.supply_series","supply_series.id","=","supply_item.series_id")->
        leftJoin("anhui_data.supply_product_item","supply_product_item.supply_item_id","=","supply_item.erp_id")->
        where("supply_product_item.car_model_id",$car_model_id)->
        where("supply_product_item.status",1)->
        where("supply_item.status",1)->
        // where("supply_series.status",1)->
        where("supply_brand.status",1)->
        where("supply_item.supply_classification_id",$supply_subject);
        // if(sizeof($tuijian)>0){ //如果说改成行存在推荐
        // 	$ddi=clone $temp_dsi;
        // 	$ddi=$ddi->whereIn("supply_item.id",$tuijian);
        // 	$ddi=$ddi->select("supply_item.id","supply_item.desc")->orderBy("supply_item.id","asc")->first();
        // }
        // if(!isset($ddi)||is_null($ddi)){//如果不存在 推荐或者 推荐的为空 按照是适配 查找
        $ddi=$temp_dsi->select("supply_item.id","supply_item.erp_id","supply_item.small_desc","supply_product_item.number","supply_product_item.max_number","supply_product_item.is_supplier","supply_product_item.isdefualt")->orderBy("supply_product_item.isdefualt","desc")->first();
        // }
        if($ddi){
            $item_info=SupplyItem::getCacheItemInfo($ddi->id);
            $dsii=DataSupplyItemIcon::where("supply_item_id",$ddi->erp_id)->where("status",1)->first();
            $ddi->model=$item_info->model;
            $ddi->brand_id=$item_info->brand_id;
            $ddi->brand_name=$item_info->brand_name;
            $ddi->series_id=$item_info->series_id;
            $ddi->series_name=$item_info->series_name;
            $ddi->subject=$item_info->supply_classification_id;
            $ddi->subject_name=$item_info->subject_name;
            // $ddi->number=isset($dmcc)&&$dmcc->number!=null?$dmcc->number:$ddi->number;
            // $ddi->max_number=isset($dmcc)&&$dmcc->number!=null?$dmcc->number:$ddi->max_number;
            $ddi->icon=isset($dsii)?$dsii->icon:"";
            $ddi->small_desc=$ddi->small_desc;
            $ddi->amount=$item_info->amount?$item_info->amount:0;
            $ddi->unit=$item_info->unit?$item_info->unit:"";
            $ddi->name=$item_info->name;
            $ddi->is_supplier=$ddi->is_supplier;
            $ddi->isdefualt=$ddi->isdefualt;
            return $ddi;
        }else{
            return null;
        }

        // $supply_arr=array(
        //     "id"=>$array->id,
        //     "model"=>$array->model,
        //     "brand_id"=>$array->brand_id,
        //     "brand_name"=>$array->brand_name,
        //     "series_id"=>$array->series_id,
        //     "series_name"=>$array->name,
        //     "subject"=>$array->subject,
        //     "subject_name"=>$array->ssb_name,
        //     "number"=>$array->number,
        //     "max_number"=>$array->max_number,
        //     "icon"=>$array->icon,
        //     "small_desc"=>$array->small_desc,
        //     "desc"=>$array->desc,
        //     "amount"=>$array->amount,
        //     "unit"=>$array->sale_unit,
        // );
        // return isset($arr[0])?$arr[0]:null;
    }


    /**
     * @Author:      sqq
     * @DateTime:    2018-11-01 11:47:06
     * 根据车型id和耗材分类id 获取 一条默认的耗材信息【cms的耗材信息 也既时anhui库中的耗材信息】 C端3.0使用
     * @param int $car_model_id     车型id
     * @param int $supply_subject   耗材分类id  [anhui库中]
     * @return array | null
     */
    public static function getDefaultC($car_model_id,$supply_subject){
        $dmcc=DataCarMaintenanceClassificationConfig::where("car_model_id",$car_model_id)->where("supply_classification_id",$supply_subject)->where("status",1)->first();
        $temp_dsi=SupplyItem::
        leftJoin("anhui_data.supply_brand","supply_brand.id","=","supply_item.brand_id")->
        leftJoin("anhui_data.supply_series","supply_series.id","=","supply_item.series_id")->
        leftJoin("anhui_data.supply_product_item","supply_product_item.supply_item_id","=","supply_item.erp_id")->
        where("supply_product_item.car_model_id",$car_model_id)->
        where("supply_product_item.status",1)->
        where("supply_item.status",1)->
        where("supply_brand.status",1)->
        where("supply_item.supply_classification_id",$supply_subject);
        // if(!isset($ddi)||is_null($ddi)){//如果不存在 推荐或者 推荐的为空 按照是适配 查找
        $ddi=$temp_dsi->select("supply_item.id","supply_item.erp_id","supply_item.small_desc","supply_product_item.number","supply_product_item.max_number")->orderBy("supply_product_item.isdefualt","desc")->get();
        // }
        if(!is_null($ddi)){
            foreach($ddi as $key => $val){
                $item_info=SupplyItem::getCacheItemInfo($val->id);
                $dsii=DataSupplyItemIcon::where("supply_item_id",$val->erp_id)->where("status",1)->first();
                $ddi[$key]->model=$item_info->model;
                $ddi[$key]->brand_id=$item_info->brand_id;
                $ddi[$key]->brand_name=$item_info->brand_name;
                $ddi[$key]->series_id=$item_info->series_id;
                $ddi[$key]->series_name=$item_info->series_name;
                $ddi[$key]->subject=$item_info->supply_classification_id;
                $ddi[$key]->subject_name=$item_info->subject_name;
                $ddi[$key]->icon=isset($dsii)?$dsii->icon:"";
                $ddi[$key]->small_desc=$val->small_desc;
                $ddi[$key]->amount=$item_info->amount?$item_info->amount:0;
                $ddi[$key]->unit=$item_info->unit?$item_info->unit:"";
                $ddi[$key]->name=$item_info->name;
            }
            return $ddi;
        }else{
            return null;
        }
    }


    /**
     * @Author:      lyt
     * @DateTime:    2016-04-22 10:34:18
     * @Description: 根据 id 获得item的 详细信息
     */
    public static function getCacheItemInfo($item_id){
        if(Cache::has('supply_item_'.$item_id)){//如果有缓存的 标准耗材
            return Cache::get('supply_item_'.$item_id);
        }else{
            $si_arr=SupplyItem::leftJoin("anhui_data.supply_series","anhui_data.supply_series.id","=","supply_item.series_id")
                ->leftJoin("anhui_data.supply_brand","supply_brand.id","=","supply_item.brand_id")
                ->leftJoin("anhui_data.supply_unit as bz_unit","bz_unit.id","=","supply_item.unit_id")
                ->leftJoin("anhui_data.supply_unit as sale_unit","sale_unit.id","=","supply_item.sale_unit_id")
                // ->leftJoin(DB::raw("(select * from anhui.supply_sub where `status`=1) as supply_sub"), 'supply_sub.supply_id',"=","supply_item.id")
                ->leftJoin("supply_classification","supply_classification.id","=","supply_item.supply_classification_id")->
                select("supply_item.id","supply_item.code","supply_item.old_code","supply_item.supply_classification_id","supply_item.supply_classification_id as subject","supply_classification.name as subject_name","supply_item.brand_id","supply_brand.brand_name","supply_item.series_id","supply_series.name as series_name","sale_unit.name as unit","supply_item.model","bz_unit.name as bz_unit","supply_item.volume_size as pack","supply_item.status","supply_item.car_model_comments","supply_item.supply_classification_id as st_classifi_id","supply_classification.name as scf_name","supply_item.name","supply_item.type")->where("supply_item.id",$item_id)->first();
            $ssub=SupplySub::where("supply_id",$item_id)->where("status",1)->first();
            if($si_arr){
                $si_arr["specification"]="";
                $si_arr["amount"]=isset($ssub->usual_price)?$ssub->usual_price:0;
                Cache::add('supply_item_'.$item_id,$si_arr, 10);
            }
            return $si_arr;
        }

    }

}